package com.java.jetpackdemo.savestate

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.SavedStateViewModelFactory
import androidx.lifecycle.ViewModelProviders
import com.java.jetpackdemo.R
import com.java.jetpackdemo.databinding.ActivitySaveStateBinding

class SaveStateActivity : AppCompatActivity() {
    private lateinit var binding: ActivitySaveStateBinding
    private lateinit var viewModel: SaveStateViewModel
    companion object {
        const val KEY_NUMBER = "KEY_NUMBER"
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //用databinding来绑定布局
        binding = DataBindingUtil.setContentView(this, R.layout.activity_save_state)
        //创建viewmodel
        viewModel = ViewModelProviders.of(this, SavedStateViewModelFactory(application, this))
            .get(SaveStateViewModel::class.java)
        //将viewmodel绑定到databinding上
        binding.data = viewModel
        //databinding监听activity的生命周期，实时更新数据
        binding.lifecycleOwner = this
    }
}
